/**
 * 
 */
package com.luoran.kaka.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.luoran.kaka.entity.ExtraInfo;
import com.luoran.kaka.service.IExtraInfoService;

/**
 * ClassName: ExtraInfoAction
 * date: 2018年12月4日 上午11:48:12
 * @author zq
 * @since JDK 1.8
 */
@Controller
@RequestMapping("/extra")
public class ExtraInfoAction {
	
	@Autowired
	IExtraInfoService extraInfoService;
	
	/**
	 *ExtraInfoAction.java:
	 */
	@RequestMapping("/add")
	@ResponseBody
	public JSONObject add(HttpServletRequest request) {
		String data=request.getParameter("data");
		JSONObject obj=new JSONObject();
		String extraId=extraInfoService.addExtraInfo(data);
		if(!StringUtils.isEmpty(extraId)) {
			obj.put("extraId", extraId);
			obj.put("flag", true);
			obj.put("type", "add");
		}else {
			obj.put("flag", false);
		}
		return obj;
	}
	
	@RequestMapping("/remove/{id}")
	@ResponseBody
	public JSONObject remove(@PathVariable("id") String id) {
		JSONObject obj=new JSONObject();
		int result=extraInfoService.deleteById(id);
		if(result==1) {
			obj.put("flag", true);
			obj.put("type", "remove");
		}else {
			obj.put("flag", false);
		}
		return obj;
		
	}
	
	@RequestMapping("/update/{extraInfoId}")
	@ResponseBody
	public JSONObject update(@PathVariable("extraInfoId")String extraInfoId,HttpServletRequest request) {
		JSONObject obj=new JSONObject();
		String extraInfoStr=request.getParameter("data");
		ExtraInfo extraInfo = extraInfoService.getExtraInfoById(extraInfoId);
		extraInfo.setExtraInfo(extraInfoStr);
		int result=extraInfoService.updateById(extraInfo);
		if(result==1) {
			obj.put("flag", true);
			obj.put("extraId", extraInfo.getId());
			obj.put("type", "update");
		}else {
			obj.put("flag", false);
		}
		return obj;
	}
}
